<?php
/**
 *
 *
 */
class RestrictionsController extends AppController
{
    public $name = "Restrictions";
    public $uses = array('Privilege', 'Restriction', 'RestrictionGrant');

    protected $grants;



    public function restrictionsmgnt()
    {
        $this->Authenticate->checkMyLogin(14);
        $this->theme = "admin";
        $error = null;

        // process form request
        if ($this->data['flag']) {
            switch($this->data['flag']){
                case "delete":
                    $this->Authenticate->checkMyLogin(17);
                    foreach($this->data['row']['id'] as $id){
                        $result = $this->Privilege->_save(array('active' => 0), $id);
                        if (is_string($result)) {
                            $error[] = $result;
                        }

                        if (is_numeric($result)) {
                            $error = "deleted";
                        }
                    }
                    break;
            } // switch
        }

        $limit = isset($this->data['limit'])?$this->data['limit']:20;
        $this->paginate['conditions'][] = array('Privilege.active' => 1);
        $this->paginate['limit'] = $limit;
        $this->paginate['order'] = array('Privilege.name' => 'ASC');
        $data = $this->paginate('Privilege');


        self::set('error', $error);
        self::set('data', $data);
        self::set('limit', $limit);
        self::set('cnt', count($data));
    }

    public function restrictiondetails($rid = 0)
    {
        $this->theme = "admin";
        $error = null;
        $msg = null;


        switch ($this->data['flag']) {
        	case "add":
                $this->Authenticate->checkMyLogin(15);
                if ($this->data['privilege']) {
		            $data['Privilege']['name'] = $this->data['privilege']['name'];
		            $data['Privilege']['description'] = $this->data['privilege']['description'];

		            $success = self::create_privilege($this->data, $rid);
		            if (is_numeric($success)) {
		            	$rg_success = self::create_restrition_grants($success, $this->data['restriction']['id']);
						if (is_array($rg_success)) {
	                		$msg[] = "Error in implementing restrictions";
	                	} else {
                			if ($rid == 0) {
                				$msg[] = "You have successfully added privilege and restriction";
                			} else {
                				$msg[] = "You have successfully updated selected privilege";
                			}
	                	}

		            } else {
		                if ($success == "existing") {
		                    $alert = "Duplicate entry";
		                } else {
		                    $alert = $success;
		                }

		                $msg = null;
		            }
		        }

        		// clear http post
		        unset($this->data['privilege']);
        		break;
        }

		if ($rid > 0) {
            $this->Authenticate->checkMyLogin(27);
            $priv_details = $this->Privilege->get_privilege_info('first', array('privilegeId' => $rid), array('*'));
            $update['name'] = $priv_details['Privilege']['name'];
            $update['description'] = $priv_details['Privilege']['description'];

            // privilege access
            $this->grants = $this->RestrictionGrant->get_restriction_grants(array('privilegeId' => $rid, 'active' => 1), 'all');
        }

        $rest_category = $this->Restriction->get_category();
        $restrictions = $this->Restriction->get_restrictions(array('active' => 1));

        self::set('restriction_category', $rest_category);
        self::set('restrictions', $restrictions);
        self::set('rid', $rid);
        self::set('error', $error);
        self::set('msg', $msg);
        self::set('update', isset($update)?$update:null);
        self::set('grants', isset($this->grants)?$this->grants:null);
        self::set('page_title', ($rid == 0)?"Add Privilege":"Edit Privilege");
    }





    /*
     * protected methods
     */
    protected function create_privilege($data, $rid)
    {
        // bypass this check if request is for update
        if ($rid == 0) {
            $conditions = array('name' => $data['privilege']['name']);
            $fields = array('privilegeId');
            $unique = $this->Privilege->get_privilege_info('first', $conditions, $fields);
        } else {
            $unique = null;
        }

        if ($unique) {
            return "existing";
        } else {
            $data['privilege']['active'] = 1;
            $data['privilege']['date_added'] = date('Y-m-d H:i:s');

            $pId = $this->Privilege->_save($data['privilege'], $rid);
            return $pId;
        }
    }

    protected function create_restrition_grants($pid = 0, $restrictions)
    {
		foreach ($restrictions as $v) {
			if ($pid == 0) {
				$rgId = $this->RestrictionGrant->_save(array('privilegeId' => $pid, 'restrictionId' => $v, 'active' => 1));
			} else {
				$rgId = $this->RestrictionGrant->__replace($pid, $v, 1);
			}

			if (is_numeric($rgId)|| is_array($rgId)) {
				$rg_success = true;
			} else {
				$rg_success[] = $v;
			}
		}

    	// this code block will update the remove the selected
    	// restrictions on the selected privilege
    	$raw_ref = $this->RestrictionGrant->get_restriction_grants(array('privilegeId' => $pid, 'active' => 1), 'all');
		foreach ($raw_ref as $r) {
			$ref[] = $r['RestrictionGrant']['restrictionId'];
		}

		$to_update = array_diff($ref, $restrictions);
		foreach ($to_update as $tu) {
			$rgId = $this->RestrictionGrant->query("update `restriction_grants` set `active`=0 where privilegeId={$pid} and `restrictionId`={$tu}");
		}
    }
}
?>